/**
 * @description  归因明细(归因分析)
 * @author shenhong
 */
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { TableC } from '../../../../comps';

class AttributeInfo extends PureComponent {
  constructor() {
    super();
    this.state = {};
  }
  componentDidMount() {
    const { dispatch, args } = this.props;
    dispatch({
      type: 'attributionAnalysisM/getAttributeInfo',
      payload: args,
    });
  }
  UNSAFE_componentWillReceiveProps(nextProps) {
    const { dispatch, args } = nextProps;
    if (JSON.stringify(args) !== JSON.stringify(this.props.args)) {
      dispatch({
        type: 'attributionAnalysisM/getAttributeInfo',
        payload: args,
      });
    }
  }
  // 对子表头的内容做优化
  setChildColumns = (children) => {
    let child = children;
    if (child && child.length) {
      child.map((item) => {
        item.align = 'right';
        item.render = (text, record) => {
          return text !== null && text !== undefined ? Number(text).toFixed(this.props.args.setupModel == '1' ? 4 : 2) : '--';
        };
      });
    }
    return child;
  };

  // 优化columns展示
  changeColumns = (columns) => {
    let newColumns = columns;
    if (newColumns && newColumns.length) {
      newColumns.map((item) => {
        if (item.title == '组合' || item.title == '基准' || item.title == '超额' || item.title == '归因分解') {
          item.align = 'center';
          item.children = this.setChildColumns(item.children);
        }
      });
    }
    return newColumns;
  };

  // 展示表格
  showTable = () => {
    const {
      attributionAnalysisM: { info },
      loading,
    } = this.props;
    const columns = this.changeColumns(info.columns);
    const dataSource = info.dataSource;
    return (
      <div style={{ height: '423px' }}>
        <TableC columns={columns} dataSource={dataSource} rowKey='id' hidePage={true} scroll={{ x: 'max-content' }} loading={loading} />
      </div>
    );
  };

  render() {
    return (
      <div>
        <>{this.showTable()}</>
      </div>
    );
  }
}
export default connect(({ attributionAnalysisM, loading }) => ({
  attributionAnalysisM,
  loading: loading.effects['attributionAnalysisM/getAttributeInfo'],
}))(AttributeInfo);
